package com.ruoyi.product.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.product.domain.BrandCategory;
import com.ruoyi.product.service.IBrandCategoryService;
import com.ruoyi.product.vo.BrandCategoryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 品牌分类关联Controller
 *
 * @author syl
 * @date 2025-08-11
 */
@RestController
@RequestMapping("/brand/category")
public class BrandCategoryController extends BaseController {
    @Autowired
    private IBrandCategoryService brandCategoryService;

    /**
     * 查询品牌分类关联列表
     */
    @RequiresPermissions("product:brand/category:list")
    @GetMapping("/list")
    public TableDataInfo list(BrandCategory brandCategory) {
        startPage();
        List<BrandCategory> list = brandCategoryService.selectBrandCategoryList(brandCategory);
        List<BrandCategoryVo> listVo = brandCategoryService.listBrandCategoryListVo(list);
        return getDataTable(listVo);
    }

    /**
     * 导出品牌分类关联列表
     */
    @RequiresPermissions("product:brand/category:export")
    @Log(title = "品牌分类关联", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BrandCategory brandCategory) {
        List<BrandCategory> list = brandCategoryService.selectBrandCategoryList(brandCategory);
        ExcelUtil<BrandCategory> util = new ExcelUtil<BrandCategory>(BrandCategory.class);
        util.exportExcel(response, list, "品牌分类关联数据");
    }

    /**
     * 获取品牌分类关联详细信息
     */
    @RequiresPermissions("product:brand/category:query")
    @GetMapping(value = "/{brandId}")
    public AjaxResult getInfo(@PathVariable("brandId") Long brandId) {
        return success(brandCategoryService.selectBrandCategoryByBrandId(brandId));
    }

    /**
     * 新增品牌分类关联
     */
    @RequiresPermissions("product:brand/category:add")
    @Log(title = "品牌分类关联", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BrandCategory brandCategory) {
        return toAjax(brandCategoryService.insertBrandCategory(brandCategory));
    }

    /**
     * 修改品牌分类关联
     */
    @RequiresPermissions("product:brand/category:edit")
    @Log(title = "品牌分类关联", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BrandCategory brandCategory, long oldCategoryId) {
        return toAjax(brandCategoryService.updateBrandCategory(brandCategory, oldCategoryId));
    }

    /**
     * 删除品牌分类关联
     */
    @RequiresPermissions("product:brand/category:remove")
    @Log(title = "品牌分类关联", businessType = BusinessType.DELETE)
    @DeleteMapping("/{brandId}")
    public AjaxResult remove(@RequestBody Long[] categoryIds,@PathVariable Long brandId) {
        return toAjax(brandCategoryService.deleteBrandCategoryByBrandIds(categoryIds, brandId));
    }
}
